<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/6/12
 * Time: 19:33

 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com

 */


namespace app\home\controller;


use app\common\model\AmountLog;
use app\common\model\UserCommission;
use app\Request;


class Statistics
{

    public  function  income(Request $request){
        $userModel  = new \app\common\model\User();
        $yearTime  = strtotime(date('Y-01-01 00:00:00'));
        $monthTime  = strtotime(date('Y-m-01 00:00:00'));
        $todayTime  = strtotime(date('Y-m-d 00:00:00'));
        $firstUser = $userModel->where(['pid'=> $request->userInfo['id']])->select()->toArray();
        $info['total_commission'] =  bcadd($request->userInfo['first_commission_income'],$request->userInfo['second_commission_income']) ;
        $info['team_count'] = 0;
        $info['team_today_count'] = 0;
        if ($firstUser){
            $info['team_count'] = count($firstUser);
            foreach ($firstUser as $firstValue){
                if (strtotime($firstValue['create_time']) > $todayTime ){
                    $info['team_today_count'] += 1;
                }
            }
            $firstUserId  = array_column($firstUser,'id');
            $secondUser =$userModel->where('pid','in', $firstUserId)->select()->toArray();

            if ($secondUser){
                $info['team_count'] += count($secondUser);
                foreach ($secondUser as $secondValue){
                    if (strtotime($secondValue['create_time']) >$todayTime ){
                        $info['team_today_count'] += 1;
                    }
                }
            }
        }
        $userCommissionModel = new UserCommission();
        $info['year_amount'] =$userCommissionModel->where(['receive_uid' => $request->userInfo['id']])
            ->where('create_time','>=',$yearTime)->sum('amount');
        $info['month_amount'] =$userCommissionModel->where(['receive_uid' => $request->userInfo['id']])
            ->where('create_time','>=',$monthTime)->sum('amount');
        $info['today_amount'] =$userCommissionModel->where(['receive_uid' => $request->userInfo['id']])
            ->where('create_time','>=',$todayTime)
            ->sum('amount');
        $amountModel  = new AmountLog();

        $info['today_income'] =$amountModel->where(['uid' => $request->userInfo['id']])
            ->where('amount_direction','=',2)
            ->where('amount_type','=',1)
            ->where('create_time','>=',$todayTime)->sum('amount');
        $info['total_income'] =$amountModel->where(['uid' => $request->userInfo['id']])
            ->where('amount_direction','=',2)
            ->where('amount_type','=',1)
           ->sum('amount');

        return success('我的团队',$info);

    }

}